package com.ffcs.crmd.tsp.core.util;

import com.ffcs.crmd.tsp.api.dto.client.TspCluster;
import com.ffcs.crmd.tsp.api.dto.client.TspClusterInstance;
import com.ffcs.crmd.tsp.api.dto.client.TspJob;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetail;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailDeath;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailParam;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailRetry;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailRunning;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailThreadInfo;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailTrack;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailWaiting;
import com.ffcs.crmd.tsp.api.dto.client.TspJobGroup;
import com.ffcs.crmd.tsp.api.dto.client.TspJobIdentityInfo;
import com.ffcs.crmd.tsp.api.dto.client.TspSlice;
import com.ffcs.crmd.tsp.api.dto.client.TspSliceGroup;
import com.ffcs.crmd.tsp.api.dto.client.TspTaskConnectInfo;
import com.ffcs.crmd.tsp.api.dto.client.TspTaskIdentity;
import com.ffcs.crmd.tsp.api.dto.client.TspThreadInfo;
import com.ffcs.crmd.tsp.api.dto.enu.JobDetailRunStatus;
import com.ffcs.crmd.tsp.api.dto.enu.JobDetailState;
import com.ffcs.crmd.tsp.api.dto.enu.JobMisfireInstruction;
import com.ffcs.crmd.tsp.api.dto.enu.JobRecover;
import com.ffcs.crmd.tsp.api.dto.enu.JobRepeatType;
import com.ffcs.crmd.tsp.api.dto.enu.JobRunExceptionType;
import com.ffcs.crmd.tsp.api.dto.enu.JobTimeoutPolicy;
import com.ffcs.crmd.tsp.api.dto.enu.JobType;
import com.ffcs.crmd.tsp.api.dto.heart.ClientData;
import com.ffcs.crmd.tsp.api.dto.heart.HeartbeatData;
import com.ffcs.crmd.tsp.api.dto.heart.TaskData;
import com.ffcs.crmd.tsp.api.dto.job.TspJobDetailShell;
import com.ffcs.crmd.tsp.api.dto.job.TspJobExecutionContext;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.dto.page.PageInfo;
import com.ffcs.crmd.tsp.api.dto.page.RetVo;
import com.ffcs.crmd.tsp.api.dto.task.PriorityWeight;
import com.ffcs.crmd.tsp.api.dto.task.TaskThreadPoolInfo;
import com.ffcs.crmd.tsp.api.dto.task.TspJobDataMap;
import com.ffcs.crmd.tsp.api.dto.task.TspJobExecutionException;
import com.ffcs.crmd.tsp.api.dto.task.TspJobInstance;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.Dom4JDriver;

/**
 *   
 * 功能说明:Xml转换工具类
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2016年1月8日 上午11:29:25
 *
 *
 * 版本号  |   作者   |  修改时间   |   修改内容
 *
 */
public class XmlUtil {
    
    private static XStream xstream = new XStream(new Dom4JDriver());
    
    protected XmlUtil() {
        
    }
    
    static {
        xstream.autodetectAnnotations(true);
        xstream.processAnnotations(TspClusterInstance.class);
        xstream.processAnnotations(TspCluster.class);
        xstream.processAnnotations(TspJobDetailTrack.class);
        xstream.processAnnotations(TspJobDetailDeath.class);
        xstream.processAnnotations(TspJobDetailParam.class);
        xstream.processAnnotations(TspJobDetailRetry.class);
        xstream.processAnnotations(TspJobDetailRunning.class);
        xstream.processAnnotations(TspJobDetailThreadInfo.class);
        xstream.processAnnotations(TspJobDetail.class);
        xstream.processAnnotations(TspJobDetailWaiting.class);
        xstream.processAnnotations(TspJobGroup.class);
        xstream.processAnnotations(TspJobIdentityInfo.class);
        xstream.processAnnotations(TspJob.class);
        xstream.processAnnotations(TspSliceGroup.class);
        xstream.processAnnotations(TspSlice.class);
        xstream.processAnnotations(TspTaskConnectInfo.class);
        xstream.processAnnotations(TspTaskIdentity.class);
        xstream.processAnnotations(JobDetailRunStatus.class);
        xstream.processAnnotations(JobDetailState.class);
        xstream.processAnnotations(JobMisfireInstruction.class);
        xstream.processAnnotations(JobRecover.class);
        xstream.processAnnotations(JobRepeatType.class);
        xstream.processAnnotations(JobRunExceptionType.class);
        xstream.processAnnotations(JobTimeoutPolicy.class);
        xstream.processAnnotations(JobType.class);
        xstream.processAnnotations(ClientData.class);
        xstream.processAnnotations(HeartbeatData.class);
        xstream.processAnnotations(TaskData.class);
        xstream.processAnnotations(TspJobDetailShell.class);
        xstream.processAnnotations(TspJobExecutionContext.class);
        xstream.processAnnotations(RemotingCommand.class);
        xstream.processAnnotations(PageInfo.class);
        xstream.processAnnotations(RetVo.class);
        xstream.processAnnotations(PriorityWeight.class);
        xstream.processAnnotations(TaskThreadPoolInfo.class);
        xstream.processAnnotations(TspJobDataMap.class);
        xstream.processAnnotations(TspJobInstance.class);
        xstream.processAnnotations(TspThreadInfo.class);
        xstream.processAnnotations(TspJobExecutionException.class);
        
        /*xstream.processAnnotations(ExcludeDay.class);
        xstream.processAnnotations(MisfireInstruction.class);
        xstream.processAnnotations(RepeatType.class);
        xstream.processAnnotations(com.ffcs.crmd.tsp.common.exception.TspJobExecutionException.class);
        xstream.processAnnotations(com.ffcs.crmd.tsp.common.enumeration.JobType.class);
        xstream.processAnnotations(com.ffcs.crmd.tsp.common.job.entity.TspJobDetail.class);
        xstream.processAnnotations(com.ffcs.crmd.tsp.common.job.entity.TspJobDataMap.class);
        xstream.processAnnotations(com.ffcs.crmd.tsp.task.job.api.TspJobExecutionContext.class);*/
        xstream.ignoreUnknownElements();
    }
    
    /**
     * 
     * 功能说明:对象转化成xml格式的字符串
     * 
     * @param object
     *          需要转换的对象
     * @author ZHONGFUHUA
     *
     * @Date 2016年1月8日
     *
     */
    public static String toXml(Object object) {
        return xstream.toXML(object);
    }
    
    /**
     * 
     * 功能说明:对象转化成xml格式的字符串
     * 
     * @param object
     *          需要转换的对象
     * @param aliasName
     *          别名
     * @author ZHONGFUHUA
     *
     * @Date 2016年1月8日
     *
     */
    public static String toXml(Object object, String aliasName) {
        xstream.aliasType(aliasName, object.getClass());
        return xstream.toXML(object);
    }
    
    /**
     * 
     * 功能说明:xml格式的字符串转化成对象
     * 
     * @param xml
     *        字符串内容
     * @param classType
     *        java类类型
     * @param <T>
     * 
     * @author ZHONGFUHUA
     * @param <T>
     *
     * @Date 2016年1月8日
     *
     */
    @SuppressWarnings("unchecked")
    public static <T> T fromXml(String xml, Class<T> classType) {
        xstream.aliasType(classType.getSimpleName(), classType);
        return (T) xstream.fromXML(xml);
    }
    
    /**
     * 
     * 功能说明:xml格式的字符串转化成对象
     * 
     * @param xml
     *        字符串内容
     * @param aliasName
     *        别名
     * @param classType
     *        java类类型
     * @param <T>
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年1月8日
     *
     */
    public static <T> Object fromXml(String xml, String aliasName, Class<T> classType) {
        xstream.aliasType(aliasName, classType);
        return xstream.fromXML(xml);
    }
    
}
